'use strict';

const Controller = require('egg').Controller;

class BookController extends Controller {
  async banner() {
    this.ctx.body = [
      {
        id: '00000001',
        text: '赢的人生的六种能力',
        image: 'https://img14.360buyimg.com/da/jfs/t1/29606/29/3235/68009/5c24a36aE78d77c33/a313c2fc797ccc98.jpg!q95.webp'
      },
      {
        id: '00000002',
        text: '实话实说-崔永元',
        image: 'https://img13.360buyimg.com/da/jfs/t1/17270/2/3824/58670/5c2c13caE230ce6ca/4a830d1e4649704e.jpg!q95.webp'
      },
      {
        id: '00000003',
        text: '我们都是宇宙中的微尘',
        image: 'http://img56.ddimg.cn/9002790053016276.jpg'
      },
      {
        id: '00000004',
        text: '所有的遇见终将连接奇丽的一生',
        image: 'http://img56.ddimg.cn/9002820077193186.jpg'
      },
      {
        id: '00000005',
        text: '万人如海一身藏',
        image: 'https://img.alicdn.com/imgextra/i2/859515618/O1CN01XwgZpd1rN5aA75caQ_!!859515618.jpg'
      },
    ];
  }

  async detail() {
      const { low, high } = this.ctx.query;
      let data = [
        {
          id: '00000011',
          price: '192.60',
          description: '大型交响组歌 中国精神',
          comment: '4300+',
          isCollected: false,
          image: '//img8.xinhuashudian.com/images/2021/08/25/5eeac1b7-d631-4171-a623-f6ca755a4080.jpg?x-oss-process=image/resize,m_mfit,h_750,w_750/format,webp'
        },
        {
          id: '00000022',
          price: '84.00',
          description: '风雨砥砺：邓小平与中国共产党百年历程',
          comment: '2000+',
          isCollected: false,
          image: '//img6.xinhuashudian.com/images/2021/07/08/7a9de8dd-4edf-428c-9bf1-42bb164b2681.jpg?x-oss-process=image/resize,m_mfit,h_750,w_750/format,webp'
        },
        {
          id: '00000033',
          price: '65.00',
          description: '永恒的初心——中国共产党成立100周年连环画集（共6册）',
          comment: '886+',
          isCollected: false,
          image: '//img8.xinhuashudian.com/images/2021/04/26/a9107359-7784-47a2-a7d4-5ed46d7fbb0e.png?x-oss-process=image/resize,m_mfit,h_750,w_750/format,webp'
        },
        {
          id: '00000044',
          price: '49.00',
          description: '西南联大求学日记',
          comment: '108+',
          isCollected: false,
          image: '//img9.xinhuashudian.com/images/5146339-fm.jpg?x-oss-process=image/resize,m_mfit,h_750,w_750/format,webp'
        },
        {
          id: '00000055',
          price: '168.00',
          description: '侵华日军兵要地志揭秘：100年来日本对中国的战场调查',
          comment: '200+',
          isCollected: false,
          image: '//img2.xinhuashudian.com/images/2021/09/08/4c8eec57-930a-4244-9d07-a974de86098f.jpg?x-oss-process=image/resize,m_mfit,h_750,w_750/format,webp'
        },
        {
          id: '00000066',
          price: '24.20',
          description: '郦波品曾国藩教子',
          comment: '220+',
          isCollected: false,
          image: '//img7.xinhuashudian.com/images/2021/09/08/c3a745dd-6c95-4b9e-b7f0-17940a02a8bc.jpg?x-oss-process=image/resize,m_mfit,h_750,w_750/format,webp'
        },
        {
          id: '00000077',
          price: '66.00',
          description: '易中天 读唐诗',
          comment: '44万+',
          isCollected: false,
          image: '//img13.360buyimg.com/n1/jfs/t1/16181/19/499/269605/5c0a47b8E15de5023/de35419e4972f5b5.jpg'
        },
        {
          id: '00000088',
          price: '79.00',
          description: '纪念碑（上下卷）',
          comment: '2000+',
          isCollected: false,
          image: '//img7.xinhuashudian.com/images/2021/09/08/427bde85-df08-46ab-b18b-9bee10e3746c.jpg?x-oss-process=image/resize,m_mfit,h_750,w_750/format,webp'
        }
      ];
      this.ctx.body = data.filter((item) => {
        // console.log(item[type], key)
        return ((!low &&!high) || (Number(item.price) <= high && Number(item.price) >= low));
      })
  }

  async add() {
    const a = [{
      id: '001',
      title: '舌尖上的中国第一季',
      label: '美食',
      author: '张三'
    },{
      id: '002',
      title: '舌尖上的中国第二季',
      label: '美食',
      author: '李四'
    },{
      id: '003',
      title: 'vue开发教程',
      label: '计算机',
      author: '王五'
    }];
    a.map(async item => {
      await this.ctx.model.Article.create(item);
    });
  }

  async getArticles() {
    let { page, key } = this.ctx.query;
    if (!key) {
      key = ''
    }
    let total = await this.ctx.model.Article.find({title: { $regex: key, $options: "$i" }}).count()
    let data

    if (page) {
      data = await this.ctx.model.Article.find({title: { $regex: key, $options: "$i" }})
      .skip((page - 1) * 5)
      .limit(5)
      .sort({ _id: "1" });
    } else {
      data =  await this.ctx.model.Article.find({title: { $regex: key, $options: "$i" }})
    }
    this.ctx.body = {
      code: 1,
      msg: 'ok',
      data: {
        total,
        totalPage: Math.ceil(total/5),
        currentPage: parseInt(page),
        list: data
      }
    }
  }

  async searchArticles() {
    const { page, key } = this.ctx.query;
    let total = await this.ctx.model.Article.find({title: { $regex: key, $options: "$i" }}).count()
    let data
    if (page) {
      data = await this.ctx.model.Article.find({title: { $regex: key, $options: "$i" }})
      .skip((page - 1) * 5)
      .limit(5)
      .sort({ _id: "1" });
    } else {
      data =  await this.ctx.model.Article.find({})
    }
    this.ctx.body = {
      code: 1,
      msg: 'ok',
      data: {
        total,
        totalPage: Math.ceil(total/5),
        currentPage: parseInt(page),
        list: data
      }
    }
  }

  async addArticles() {
    const { id, title, label, author } = this.ctx.request.body;
    await this.ctx.model.Article.create({
      id,
      title,
      label,
      author
    });
    this.ctx.body = {
      code: 0,
      msg: 'ok',
      data: ""
    }
  }

  async editArticles() {
    const { id, title, label, author } = this.ctx.request.body;
    const re = await this.ctx.model.Article.updateOne(
      { id: id },
      { title, label, author }
    );

    this.ctx.body = {
      code: 0,
      msg: 'ok',
    }
  }

  async deleteArticles() {
    const { id } = this.ctx.request.body;
    // const re = await this.ctx.model.Article.find({
    //   id: id
    // }).count();
    // if (re == 0) {
    //     this.ctx.body = {
    //     code: -1,
    //     msg: 'error',
    //   }
    // } else {
    //   this.ctx.body = {
    //     code: 0,
    //     msg: 'ok',
    //   }
    // }

    const re = await this.ctx.model.Article.remove({
      id: id
    })
    this.ctx.body = {
      code: 0,
      msg: 'ok',
    }
  }
}

module.exports = BookController;